<?php

namespace app\controller\admin\myworld;


use app\model\api\WorldCategory;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use laytp\library\Tree;

/**
 * 菜单控制器
 */
class Category extends Backend
{
    public $noNeedAuth = ['getMenuTree','getTree'];
    public $menuList;
    public $model;
    public $orderRule = ['sort' => 'desc', 'id' => 'asc'];

    public function _initialize()
    {
        $this->model = new WorldCategory();
    }

    public function index()
    {
        $where = $this->buildSearchParams();
        $order = $this->buildOrder();
        $sourceData = $this->model->where($where)->order($order)->select()->toArray();
        $isTree = $this->request->param('is_tree');
        $select_id = $this->request->param('select');

        /*foreach($sourceData as $key => $value){
            if(!empty($select_id)){
                if($select_id == 1 && $value['type'] == 2){
                    $sourceData[$key]['disabled'] = true;
                }
                if($select_id == 2 && $value['type'] == 1){
                    $sourceData[$key]['disabled'] = true;
                }
            }

        }*/

        if($isTree){
            $menuTreeObj = Tree::instance();
            $menuTreeObj->init($sourceData);
            $data = $menuTreeObj->getRootTrees();
        }else{
            $data = $sourceData;
        }


        /*foreach($data as &$item){
            $item['disabled'] = true;
        }*/
        return $this->success('数据获取成功', $data);
    }

    //编辑
    public function edit()
    {
        $id   = $this->request->param('id');
        $info = $this->model->find($id);
        $post = CommonFun::filterPostData($this->request->post());
        if ($id == $post['pid']) {
            return $this->error('不能将上级改成自己');
        }
        $info->category_name=$post['name'];
        $info->pid=$post['pid'];
        $info->sort=$post['sort'];
        $update_res = $info->save();
        if ($update_res) {
            return $this->success('操作成功');
        } else if ($update_res === 0) {
            return $this->success('未做修改');
        } else {
            return $this->error('操作失败');
        }
    }

    //删除
    public function del()
    {
        $ids = $this->request->post('ids');
        if (!$ids) {
            return $this->error('参数ids不能为空');
        }

        $sourceData = $this->model->select()->toArray();
        $treeLib = Tree::instance();
        $treeLib->init($sourceData);
        $childIds = $treeLib->getChildIds($ids);

        if ($this->model->destroy($childIds)) {
            return $this->success('数据删除成功', $childIds);
        } else {
            return $this->error('数据删除失败');
        }
    }

    //设置排序
    public function setSort()
    {
        $id       = $this->request->post('id');
        $fieldVal = $this->request->post('field_val');
        $isRecycle = $this->request->post('is_recycle');
        $update['sort'] = $fieldVal;
        try {
            if($isRecycle) {
                $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
            } else {
                $updateRes = $this->model->where('id', '=', $id)->update($update);
            }
            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


    public function info(){
        $id = $this->request->param('id');
        $category = $this->model->where('id',$id)->find()->toArray();
        $category['name']   = !empty($category['category_name'])?$category['category_name']:'';
        $category['sort']   = !empty($category['sort'])?$category['sort']:'';

        return $this->success('获取成功', $category);
    }

    //添加
    public function add()
    {
        $post = CommonFun::filterPostData($this->request->post());
        $insData['category_name'] = $post['name'];
        $insData['pid'] = $post['pid'];
        $insData['sort'] = $post['sort'];
        try {
            $save = $this->model->save($insData);
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}
